﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV.Forms.KhaiBao;
using System.Data.SqlClient;

namespace QLTV.UserControl
{
    public partial class ucRank : DevExpress.XtraEditors.XtraUserControl
    {
        public ucRank()
        {
            InitializeComponent();
            statusDataBar1.addTextChange(gridView1);
            statusDataBar1.addTextChange(tblCertificationBindingSource);
            statusDataBar1.AddControlPrint(gridControl1);
        }

        private void treeList1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmEditRank f = new frmEditRank();
            f.Text = "Thêm chức danh";
            f.AddClick += f_AddClick;
            f.ShowDialog();
        }

        void f_AddClick(object sender, EventArgs e)
        {
            this.tblRankTableAdapter.Fill(dsRanking.tblRank);
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (treeList1.Selection.Count != 0)
            {
                int position = this.tblRankBindingSource.Position;
                frmEditRank f = new frmEditRank(this.tblRankBindingSource);
                f.Text = "Sửa chức danh";
                f.ShowDialog();
                this.tblRankTableAdapter.Fill(dsRanking.tblRank);
                this.tblRankBindingSource.Position = position;
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.tblRankBindingSource.RemoveCurrent();
                this.tblRankTableAdapter.Update(dsRanking.tblRank);
                this.tblRankTableAdapter.Fill(dsRanking.tblRank);
            }
            catch (SqlException ex)
            {
                if (ex.ErrorCode == -2146232060)
                {
                    XtraMessageBox.Show("Dữ liệu đang được sử dụng vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void ucRank_Load(object sender, EventArgs e)
        {
            this.tblRankTableAdapter.Fill(dsRanking.tblRank);
        }

        private void treeList1_DoubleClick(object sender, EventArgs e)
        {
            DevExpress.XtraTreeList.TreeListHitInfo hi = treeList1.CalcHitInfo(treeList1.PointToClient(Control.MousePosition));
             if (hi.Node != null)
             {
                 int position = this.tblRankBindingSource.Position;
                 frmEditRank f = new frmEditRank(this.tblRankBindingSource);
                 f.Text = "Sửa chức danh";
                 f.ShowDialog();
                 this.tblRankTableAdapter.Fill(dsRanking.tblRank);
                 this.tblRankBindingSource.Position = position;
             }
        }

        private void treeList1_Click(object sender, EventArgs e)
        {
            DevExpress.XtraTreeList.TreeListHitInfo hi = treeList1.CalcHitInfo(treeList1.PointToClient(Control.MousePosition));
            if (hi.Node != null)
            {
                this.tblCertificationTableAdapter.FillByRankId(dsCertificate.tblCertification, int.Parse(hi.Node.GetValue(colnRankId).ToString()));
            }
        }

        private void statusDataBar1_NewClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblCertificationBindingSource.AddNew();
        }

        private void statusDataBar1_UpdateClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblCertificationBindingSource.EndEdit();
            this.tblCertificationTableAdapter.Update(dsCertificate.tblCertification);
            this.tblCertificationTableAdapter.Fill(dsCertificate.tblCertification);
        }

        private void statusDataBar1_DeleteClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            try
            {
                this.tblCertificationBindingSource.RemoveCurrent();
                this.tblCertificationTableAdapter.Update(dsCertificate.tblCertification);
                this.tblCertificationTableAdapter.Fill(dsCertificate.tblCertification);
            }
            catch (SqlException ex)
            {
                DevExpress.XtraTreeList.TreeListHitInfo hi = treeList1.CalcHitInfo(treeList1.PointToClient(Control.MousePosition));
                if (hi.Node != null)
                {
                    this.tblCertificationTableAdapter.FillByRankId(dsCertificate.tblCertification, int.Parse(hi.Node.GetValue(colnRankId).ToString()));
                }
                if (ex.ErrorCode == -2146232060)
                {
                    XtraMessageBox.Show("Dữ liệu đang được sử dụng vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void statusDataBar1_UndoClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblCertificationBindingSource.CancelEdit();
            this.tblCertificationTableAdapter.Fill(dsCertificate.tblCertification);
        }
    }
}
